package assignmentstrategy

import (
	"codejy/analysis/codeanalysis"
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"codejy/utils"
	"fmt"
	"strings"
)

type InstancemethodStrategy struct {
}

func init() {
	RegisterStrategy(&InstancemethodStrategy{})
}
func (n InstancemethodStrategy) getId() int {
	return codeanalysis.InstanceMethod
}

func (n InstancemethodStrategy) Execute(threadStore *structdef.ThreadStore, code string) string {
	return store.NweStatusRestore(threadStore, func() string {
		index := strings.Index(code, ".")
		v, _, _ := store.Get3LevelVariable(threadStore, code[:index]) //获取变量实例地址
		threadStore.CurrentInstanceAddress = v.Value
		utils.DebugLog("调用实例方法时候,当前操作的实例地址", func() {
			fmt.Println(v.Value)
		})

		//获取类型方法定义
		prop, _ := store.GetInstanceProp(threadStore, v.Value)
		typeName, _ := prop["@TYPENAME@"]  //获取实例的类型名称
		scope, _ := prop["@TYPEFILEPATH@"] //获取实例的类型名称
		typeDefinition := store.GetTypeDefinition(scope.Value, typeName.Value)
		defMethod := code[index+1:]
		//解析函数调用,执行函数调用
		interfaces := globalfunc.ExecuteFunc("SubFunctionCallNameAndParam", defMethod).([]interface{})
		funName, funParams := interfaces[0].(string), interfaces[1].([]string)
		definition, _ := typeDefinition.Methods[funName]
		globalfunc.ExecuteFunc("ExecuteTypeMethod", threadStore, funName, definition, funParams) //执行方法
		//获取函数返回值
		variable1, _ := store.GetVariable(threadStore, funName)
		return variable1.Value
	})
}
